package ldh.im.fxgui.util;

import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.stage.Popup;
import ldh.im.fxbase.component.EmojiPane;
import ldh.im.fxbase.component.ScreenCapturePane;
import ldh.im.fxbase.richtext.ImTextPane;
import ldh.im.fxgui.controller.MainController;
import ldh.im.fxgui.controller.MessageController;

import java.io.File;
import java.util.function.Consumer;

public class UiUtil {

    private static final String cssLink = MessageController.class.getResource("/css/html.css").toExternalForm();
    private static final String jsLink = MessageController.class.getResource("/js/html.js").toExternalForm();
    private static final String initHtml = "<link rel=\"stylesheet\" href=\"" + cssLink +"\" /><script type=\"text/javascript\" src=\"" + jsLink + "\"></script>";

    private static final String initWebHtml = String.format("<html dir=\"ltr\"><head>%s</head><body contenteditable=\"true\"><span id=\"endSpan\"/></body></html>", initHtml);

    public static String getInitWebViewHtml() {
        return initWebHtml;
    }

    private static volatile EmojiPane emojiPane = null;
    private static volatile ScreenCapturePane screenCapturePane = null;
    private static volatile MainController mainController = null;
    private static volatile Popup MAIN_POPUP = null;

    public static void setMainController(MainController mainController) {
        UiUtil.mainController = mainController;
    }

    public static MainController getMainController() {
        return UiUtil.mainController;
    }

    public static EmojiPane getEmojiPane() {
        if (emojiPane == null) {
            synchronized (UiUtil.class) {
                if (emojiPane == null) {
                    emojiPane = new EmojiPane();
                }
            }
        }
        return emojiPane;
    }

    public static ScreenCapturePane getScreenCapturePane() {
        if (screenCapturePane == null) {
            synchronized (UiUtil.class) {
                if (screenCapturePane == null) {
                    screenCapturePane = new ScreenCapturePane();
                }
            }
        }
        return screenCapturePane;
    }

    public static Popup getMainPopup() {
        if (MAIN_POPUP == null) {
            MAIN_POPUP = new Popup();
            MAIN_POPUP.setAutoHide(true);
        }
        return  MAIN_POPUP;
    }

    public static String getDataFile(String file) {
        String root = System.getProperty("user.dir");
        String dir = root + file;
        return dir;
    }

    public static String getDataFileOrCreate(String file) {
        String root = System.getProperty("user.dir");
        String dir = root + File.separator + file;
        File dirFile = new File(dir);
        while(!dirFile.exists()) dirFile.mkdirs();
        return "file:///" + dir;
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }

    public static void uiHandle(Consumer consumer) {
        Platform.runLater(()->{
            consumer.accept(null);
        });
    }

    public static double calcHeight(String str, double width) {
        Text text = new Text(str);
        text.setWrappingWidth(width);
        new Scene(new Group(text));

        text.applyCss();

        final double height = text.getLayoutBounds().getHeight();
        return height;
    }

    public static ImTextPane createImTextFlow(String content) {
        ImTextPane textFlow = new ImTextPane();
        textFlow.setImContent(content);
        return textFlow;
    }
}
